package tool

import play.api.data._
import play.api.data.Forms._
import shared.VarTool

/**
 * Created by yz on 2018/7/17
 */

import Pojo._

class FormTool {

  case class AccountData(account: String, password: String)

  val accountForm = Form(
    mapping(
      "account" -> text,
      "password" -> text
    )(AccountData.apply)(AccountData.unapply)
  )

  case class userNameData(name: String)

  val userNameForm = Form(
    mapping(
      "name" -> text
    )(userNameData.apply)(userNameData.unapply)
  )

  val idOpForm = Form(
    mapping(
      "id" -> optional(number)
    )(IdOpData.apply)(IdOpData.unapply)
  )


  val saveConfigForm = Form(
    mapping(
      "missionName" -> text,
      "configName" -> text
    )(ConfigFormData.apply)(ConfigFormData.unapply)
  )

  val userForm = Form(
    mapping(
      "name" -> text,
      "password" -> text,
      "unit" -> text
    )(UserData.apply)(UserData.unapply)
  )

  val unitForm = Form(
    mapping(
      "unit" -> text
    )(UnitData.apply)(UnitData.unapply)
  )

  val loginUserForm = Form(
    mapping(
      "name" -> text,
      "password" -> text
    )(LoginUserData.apply)(LoginUserData.unapply)
  )

  case class ChangePasswordData(password: String, newPassword: String)

  val changePasswordForm = Form(
    mapping(
      "password" -> text,
      "newPassword" -> text
    )(ChangePasswordData.apply)(ChangePasswordData.unapply)
  )

  val missionForm = Form(
    mapping(
      "missionName" -> text,
    )(MissionData.apply)(MissionData.unapply)
  )

  case class MissionNameData(missionName: String)

  val configForm = Form(
    mapping(
      "threadNum" -> number,
      "isInter" -> text,
      "rfTop" -> text,
      "svmTop" -> text,
      "cor" -> mapping(
        "isCorExec" -> text,
        "isSelfCorExec" -> text,
        "isParCorExec" -> text,
        "coe" -> text,
        "p" -> text,
        "fdr" -> text
      )(CorData.apply)(CorData.unapply),
      "pathway" -> mapping(
        "isIPathExec" -> text,
        "isEnrichExec" -> text,
        "libTypes" -> seq(text),
        "isPathwayExec" -> text,
        "method" -> text,
        "nodeImp" -> text
      )(PathwayData.apply)(PathwayData.unapply),
      "species" -> text,
      "isSmp" -> text,
      "anaKind" -> text,
      "diagnoseIsExec" -> text,
      "coef" -> text,
      "isLoess" -> text,
      "isNormal" -> default(text, "F"),
      "preDeal" -> mapping(
        "replaceMethod" -> text,
        "knn" -> text,
        "min" -> text
      )(PreDealData.apply)(PreDealData.unapply),
      "diffMethod" -> mapping(
        "method" -> text,
        "fcMethod" -> text,
        "mulMethod" -> text,
        "pValue" -> text,
        "fdr" -> text,
        "log2FC" -> text,
        "kwP" -> text,
        "kwFdr" -> text
      )(DiffMethodData.apply)(DiffMethodData.unapply),
      "oplsda" -> mapping(
        "vip" -> text,
        "q2Y" -> text,
        "q2YB" -> text
      )(OplsdaData.apply)(OplsdaData.unapply),
      "borutaAfterTopN" -> text,
      "execCv" -> text,
      "seed" -> text,
      "k" -> text,
      "reportHome" -> mapping(
        "isTargetTest" -> text,
        "client" -> text,
        "affiliation" -> text,
        "projectCode" -> text,
        "salesRep" -> text,
        "testOrderName" -> text,
        "testOrderId" -> text,
        "sampleType" -> text,
        "unit" -> text,
      )(ReportHomeData.apply)(ReportHomeData.unapply),
      "colorSolution" -> optional(text)
    )(ConfigData.apply)(ConfigData.unapply)
  )

  val missionNameForm = Form(
    mapping(
      "missionName" -> text
    )(MissionNameData.apply)(MissionNameData.unapply)
  )

  case class AdjustMissionData(mission: String)

  val adjustMissionForm = Form(
    mapping(
      "mission" -> text,
    )(AdjustMissionData.apply)(AdjustMissionData.unapply)
  )

  val missionIdForm = Form(
    mapping(
      "missionId" -> number
    )(MissionIdData.apply)(MissionIdData.unapply)
  )

  case class MissionIdOptionData(missionId: Option[Int])

  val missionIdOptionForm = Form(
    mapping(
      "missionId" -> optional(number)
    )(MissionIdOptionData.apply)(MissionIdOptionData.unapply)
  )

  case class ArgsData(missionId: Int, compoundName: String)

  val argsForm = Form(
    mapping(
      "missionId" -> number,
      "compoundName" -> text,
    )(ArgsData.apply)(ArgsData.unapply)
  )

  case class ParamAdjustData(missionName: String, missionId: Int, compoundName: String, flMin: String, flMax: String, step: String, nups: String,
                             ndowns: String, snr: String, iteration: String, bLine: String, rtlw: String, rtrw: String,
                             rt: String, peakLocation: String)

  val paramAdjustForm = Form(
    mapping(
      "missionName" -> text,
      "missionId" -> number,
      "compoundName" -> text,
      "flMin" -> text,
      "flMax" -> text,
      "step" -> text,
      "nups" -> text,
      "ndowns" -> text,
      "snr" -> text,
      "iteration" -> text,
      "bLine" -> text,
      "rtlw" -> text,
      "rtrw" -> text,
      "rt" -> text,
      "peakLocation" -> text
    )(ParamAdjustData.apply)(ParamAdjustData.unapply)
  )

  case class UnitConversionData(unit: String, toUnit: String, fromC: Int)

  val unitConversionForm = Form(
    mapping(
      "unit" -> text,
      "toUnit" -> text,
      "fromC" -> number
    )(UnitConversionData.apply)(UnitConversionData.unapply)
  )

  case class FileNameData(fileName: String)

  val fileNameForm = Form(
    mapping(
      "fileName" -> text
    )(FileNameData.apply)(FileNameData.unapply)
  )

  case class FileNamesData(fileNames: Seq[String])

  val fileNamesForm = Form(
    mapping(
      "fileNames" -> seq(text)
    )(FileNamesData.apply)(FileNamesData.unapply)
  )

}

object FormTool {

  val idForm = Form(
    mapping(
      "id" -> number
    )(IdData.apply)(IdData.unapply)
  )

  val configNameForm = Form(
    mapping(
      "configName" -> text
    )(ConfigNameData.apply)(ConfigNameData.unapply)
  )

  val dbNameForm = Form(
    mapping(
      "dbName" -> text
    )(DbNameData.apply)(DbNameData.unapply)
  )

  val otherConfigForm = Form(
    mapping(
      "dbId" -> number,
      "helper"->optional(text)
    )(DbIdData.apply)(DbIdData.unapply)
  )


}
